<template>
	<view class="leave-examine">
		<nav-tabs-leave
			:defaultIndex="defaultIndex"
			@attedanceChange="attedanceChange"
			@workChange="workChange"
			:attedanceOptions="attedanceOptions"
			:periodValue="periodValue"
			:workGroupeOptions="workGroupeOptions"
			:tapsData="tapsData"
			@tapChange="clickTabs"
		></nav-tabs-leave>
		<view v-if="defaultIndex == 0" class="has-examine">
			<view v-if="leaveListData.length == '0'" class="data-tip">当前没有任何数据</view>
			<view v-if="leaveListData.length != '0'">
				<view v-for="(item, index) in leaveListData" class="leave-card" :key="index">
					<leave-examine-card :leaveData="item.data" @adopt="adopt" @exit="exit" @handleClick="jumpLeaveDetail"></leave-examine-card>
				</view>
			</view>
		</view>

		<view v-if="defaultIndex == 1" class="examine-inforn">
			<view v-if="leaveListData.length == '0'" class="data-tip">当前没有任何数据</view>
			<view class="roval-list" v-if="leaveListData.length != '0'">
				<view v-for="(item, index) in leaveListData" :key="index">
					<approval-leave-exa
						:status="item.data.hvo.approvenote == '驳回' ? '2' : '1'"
						:timeArr="item.data.bvos"
						:propTitlel="item.data.bvos[0].leaveshift"
						:propdays="item.data.hvo.sumhour"
						@handleClick="clickRoval(item.data)"
						:personal="item.data.hvo.pk_psndoc.name"
						@exitExa="exitExa(item.data.hvo.pk_leave_h)"
					></approval-leave-exa>
				</view>
			</view>
		</view>
		<sendNews ref="sendNews" @confirm="confirm" :status="examineStatus"></sendNews>
	</view>
</template>

<script>
import service from '../../../common/service/new_file.js';
import apiData from '../../../common/utils/apiData.js';
import utils from '../../../common/utils/utils.js';
export default {
	data() {
		return {
			examineStatus: '',
			attedanceOptions: [],
			workGroupeOptions: [],
			searchValue: '',
			periodValue: '期间',
			defaultIndex: 0,
			tapsData: [{ value: '未审核', num: 0 }, { value: '已审核', num: 0 }],
			leaveListData: [],
			rovalListData: [
				{ status: '0', propDate: { start: '2015-1-1(全天)', end: '2015-1-2(半天)' }, propTitlel: '病假', propdays: '1.5' },
				{ status: '1', propDate: { start: '2015-1-1(全天)', end: '2015-1-2(半天)' }, propTitlel: '事假', propdays: '2.5' },
				{ status: '2', propDate: { start: '2015-1-1(全天)', end: '2015-1-2(半天)' }, propTitlel: '病假', propdays: '1.5' }
			],
			navForm: {
				pk_leavetype: '',
				pk_period_b: ''
			},
			isCheck: 'N',
			nowItemPk: '', //当前选择的项
			spr: [] //指派人
		};
	},
	methods: {
		clickRoval(e) {
			console.log(e);
			uni.navigateTo({
				url: '../leaveExaDetail/leaveExaDetail?item=' + JSON.stringify(e)
			});
		},
		jumpLeaveDetail(e) {
			console.log(e);
			uni.navigateTo({
				url: '../leaveExaDetail/leaveExaDetail?item=' + JSON.stringify(e)
			});
		},
		exitExa(pk) {
			let _this = this;
			let data = apiData.setExitExamineData(pk, 'HA01'); //FN(billtype, isAdopt,pk)
			service.common(data, {
				success: function(res) {
					setTimeout(() => {
						uni.showToast({
							title: '已撤回'
						});
					}, 100);
					_this.getLeaveList();
				},
				fail: function(err) {},
				all: function(all) {}
			});
		},
		attedanceChange(value) {
			this.periodValue = value.yearmth;
			this.navForm.pk_period_b = value.pk_period_b;
			this.getLeaveList();
		}, //期间
		workChange(value) {
			this.navForm.pk_leavetype = value;
			this.getLeaveList();
		}, //类别

		confirm(value) {
			let _this = this;
			if (this.examineStatus == 1) {
				let _this = this;
				let data = apiData.salaryExamineData('HA01', true, this.nowItemPk, value, this.spr); //FN(billtype, isAdopt,pk)

				service.common(data, {
					success: function(res) {
						setTimeout(() => {
							uni.showToast({
								title: '已通过',
								duration: 1500
							});
						}, 100);
						_this.getLeaveList();
					},
					fail: function(err) {
						console.log(err);
					},
					all: function(all) {
						console.log(all);
					}
				});
			} else if (this.examineStatus == 0) {
				let data = apiData.salaryExamineData('HA01', false, this.nowItemPk, value, this.spr);
				service.common(data, {
					success: function(res) {
						setTimeout(() => {
							uni.showToast({
								title: '已驳回',
								icon: 'none',
								duration: 1500
							});
						}, 100);
						_this.getLeaveList();
					},
					fail: function(err) {
						console.log(err);
					}
				});
			}
		}, //审核意见弹出框
		adopt(obj) {
			this.nowItemPk = obj.pk;
			this.spr = obj.spr; //获取指派人
			this.examineStatus = 1;
			this.$refs.sendNews.show();
		},
		exit(obj) {
			this.nowItemPk = obj.pk;
			this.spr = obj.spr; //获取指派人
			this.examineStatus = 0;
			this.$refs.sendNews.show();
		},
		clickTabs(e) {
			this.defaultIndex = e.index;
			this.leaveListData = [];
			if (e.index == '0') {
				this.isCheck = 'N';
				this.getLeaveList();
			} else if (e.index == '1') {
				this.isCheck = 'Y';
				this.getLeaveList();
			}
		},
		getLeaveList() {
			let _this = this;
			let head = {
				pk_leavetype: this.navForm.pk_leavetype,
				pk_period_b: this.navForm.pk_period_b
			};
			function timeFormat(time) {
				let date = new Date(time.replace(/-/g, '/'));
				let yy = date.getFullYear();
				let mm = date.getMonth() + 1;
				let dd = date.getDate();
				return yy + '-' + mm + '-' + dd;
			}
			let data = apiData.setLeaveListData('HA01', this.isCheck, head);
			service.common(data, {
				success: function(res) {
					if (res.data.VALUES.length == 0) {
						uni.showToast({
							icon: 'none',
							title: '未查询到数据'
						});
					}
					res.data.VALUES.forEach(item2 => {
						item2.data.msg = item2.message; //把msg下移了一层
						item2.data.bvos.forEach(item => {
							let start;
							let endTime;
							if (item.leavebeginpro == 1) {
								start = timeFormat(item.leavebegintime) + ' ' + '全天';
							} else if (item.leavebeginpro == 2) {
								start = timeFormat(item.leavebegintime) + ' ' + '上午';
							} else if (item.leavebeginpro == 3) {
								start = timeFormat(item.leavebegintime) + ' ' + '下午';
							}

							if (item.leaveendpro == 1) {
								endTime = timeFormat(item.leaveendtime) + ' ' + '全天';
							} else if (item.leaveendpro == 2) {
								endTime = timeFormat(item.leaveendtime) + ' ' + '上午';
							} else if (item.leaveendpro == 3) {
								endTime = timeFormat(item.leaveendtime) + ' ' + '下午';
							}
							item.leavebegintime = start;
							item.leaveendtime = endTime;
						});
					});
					_this.leaveListData = res.data.VALUES;
					// uni.setStorageSync('LeaveRemineData',JSON.stringify(res.data.VALUES))
					if (_this.isCheck == 'N') {
						_this.tapsData[0].num = _this.leaveListData.length;
					} else if (_this.isCheck == 'Y') {
						// _this.tapsData[1].num = _this.leaveListData.length
					}
					// console.log(_this.leaveListData)
				}
			});
		}
	},
	onLoad() {
		this.getLeaveList();
		this.workGroupeOptions = JSON.parse(uni.getStorageSync('leaveShift'));
		this.workGroupeOptions.unshift({
			value: '',
			label: '全部'
		});
		this.attedanceOptions = JSON.parse(uni.getStorageSync('workPeriod'));
		this.attedanceOptions.unshift({
			pk_period: '',
			pk_period_b: '',
			yearmth: '全部'
		});
	},
	onPullDownRefresh() {
		this.getLeaveList();
	}
};
</script>

<style lang="scss" scoped>
.head {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 0 30rpx;
	padding-top: 120rpx;
	.search-btn {
		font-size: 28rpx;
		padding-left: 10rpx;
	}
	.search {
		flex: 1;
	}
}

.has-examine {
	padding-top: 80rpx;
	padding-bottom: 40rpx;
}
.examine-inforn {
	padding-top: 80rpx;
	padding-bottom: 40rpx;
}
.data-tip {
	padding-top: 100rpx;
	text-align: center;
	color: #949494;
	font-size: 28rpx;
}
.roval-list {
	padding: 20rpx 36rpx;
}
.leave-card {
	margin: 40rpx 0;
}
.date {
	padding: 0 36rpx;
	margin-top: 20rpx;
	font-size: 16px;
	font-weight: 600;
	color: #121f2c;
}
</style>
